package com.xlp.order;

import com.xlp.additive.Additive;
import com.xlp.drink.CustomDrink;
import com.xlp.drink.Drink;

import java.util.ArrayList;
import java.util.List;

public class DrinkOrder {
    private List<Drink> drinks = new ArrayList<>(10);

    public List<Drink> getDrinks() {
        return drinks;
    }

    public void setDrinks(List<Drink> drinks) {
        if (drinks != null)
            this.drinks = drinks;
    }

    public boolean addDrink(Drink drink){
        if (drink != null){
           return drinks.add(drink);
        }
        return false;
    }

    public double getSumPrice(){
        double sumPrice = drinks.stream().mapToDouble(Drink::getPrice).sum();
        return sumPrice;
    }

    public String getOrderDetail(){
        if (drinks.size() > 10) return "-1";
        StringBuilder builder = new StringBuilder("订单总价是：\t").append(getSumPrice()).append("元\t")
                .append("购买数量是：  ").append(drinks.size()).append("杯\n\t\t");
        drinks.forEach(item -> {
            if (item instanceof CustomDrink){
                CustomDrink drink = (CustomDrink) item;
                builder.append(drink.getDrink().getName()).append("：\t").append(drink.getDrink().getPrice())
                        .append("元\n\t\t");
                drink.getAdditives().forEach(additive ->
                        builder.append("\t\t")
                                .append(additive.getName())
                                .append("：\t").append(additive.getPrice())
                                .append("元\n\t\t")
                );
            } else {
                builder.append(item.getName()).append("：\t").append(item.getPrice()).append("元\n\t\t");
            }
        });
        return builder.toString();
    }

    @Override
    public String toString(){
        return getOrderDetail();
    }
}
